// stores/tabBar.js
import { defineStore } from 'pinia';
import { getMobileTheme } from '@/api/shop/mobileTheme';

export const useTabBarStore = defineStore('tabBar', {
	state: () => ({
		tabBar: [],
		tabbarColor: '',
		tabbarSelectedColor: '',
		tabbarBgColor: '',
		tabbarBorderStyle: '',
	}),
	actions: {
		initTabBar() {
			getMobileTheme({}).then((response) => {
				this.setTabBar(response.data);
			});
		},
		setTabBar(theme: any) {
			const tabBar = theme.tabbarItem.info;
			const tabbarColor = theme.tabbarColor;
			const tabbarSelectedColor = theme.tabbarSelectedColor;
			const tabbarBgColor = theme.tabbarBgColor;
			const tabbarBorderStyle = theme.tabbarBorderStyle;
			tabBar.forEach((item: any) => {
				uni
					.setTabBarItem({
						index: item.index,
						text: item.text,
						iconPath: '/static/' + item.iconPath,
						selectedIconPath: '/static/tabbar/' + item.selectedIconPath,
					})
					.catch((e) => {
						console.log(e);
					});
			});
			// 设置tabbar样式
			uni.setTabBarStyle({
				color: tabbarColor,
				selectedColor: tabbarSelectedColor,
				backgroundColor: tabbarBgColor,
				borderStyle: tabbarBorderStyle,
			});
		},
	},
});
